package com.zhuzi.demojfoenix;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXTimePicker;
import io.datafx.controller.ViewController;
import io.datafx.controller.flow.action.ActionMethod;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

import javax.annotation.PostConstruct;

@ViewController(value = "/com/zhuzi/demojfoenix/hello-view.fxml", title = "Material Design Example")
public class HelloController {
    @FXML
    public JFXDialog dialog;
    @FXML
    public VBox context;
    @FXML
    private Label welcomeText;

    @FXML
    // @ActionTrigger("clickBtnClick")//这个注解没生效
    private JFXButton clickBtn;

    @FXML
    JFXDatePicker dateOverlay;

    @FXML
    JFXTimePicker timeOverlay;
    private int clickCount = 0;

    @PostConstruct
    public void init() {
        welcomeText.setText("Button was clicked " + clickCount + " times");
    }

    @ActionMethod("clickBtnClick")
    public void onAction() {
        System.out.println("clicked");
        clickCount++;
        welcomeText.setText("Button was clicked " + clickCount + " times");
    }

    //onHelloButtonClick
    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("Welcome to JavaFX Application!");
    }

    // @FXML
    private StackPane dialogStackPane;

    /**
     * 弹出JFXDialog弹窗
     */
    public void alert() {
        addDialogStackPane();
        dialog.setTransitionType(JFXDialog.DialogTransition.CENTER);
        dialog.show(dialogStackPane);
    }

    /**
     * 关闭JFXDialog弹窗
     */
    public void close() {
        dialog.close();
        dialogStackPane.getChildren().remove(dialog);
        context.getChildren().remove(dialogStackPane);
    }

    /**
     * 新增JFXDialog弹窗容器
     */
    private void addDialogStackPane() {
        dialogStackPane = new StackPane();
        dialogStackPane.setPrefHeight(context.getHeight());
        dialogStackPane.setPrefWidth(context.getWidth());
        context.getChildren().add(dialogStackPane);
    }

}
